﻿using com.kmer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;

namespace com.kmer.library.Helper
{
    public static class MemberHelper
    {
        /// <summary>
        /// 判断当前用户是否有权限
        /// </summary>
        /// <param name="thePage"></param>
        /// <param name="prgItem">目标</param>
        /// <param name="rights">权限</param>
        /// <param name="isBack">是否后台用户 默认是，暂时不支持前台用户</param>
        /// <returns></returns>
        public static bool HasRights(this Page thePage, string prgItem, RightTypes rights, bool isBack = true)
        {
            return HasRights(thePage, prgItem, (long)rights, isBack);
        }

        /// <summary>
        /// 判断当前用户是否有权限
        /// </summary>
        /// <param name="thePage"></param>
        /// <param name="prgItem">目标</param>
        /// <param name="rights">权限</param>
        /// <param name="isBack">是否后台用户 默认是，暂时不支持前台用户</param>
        /// <returns></returns>
        public static bool HasRights(this Page thePage, string prgItem, long rights, bool isBack = true)
        {
            // return HasRights(UserInfo.StaffNo, prgItem, rights, isBack);

            return false;
        }
        /// <summary>
        /// 判断当前用户是否有权限
        /// </summary>
        /// <param name="thePage"></param>
        /// <param name="prgItem">目标</param>
        /// <param name="rights">权限</param>
        /// <param name="isBack">是否后台用户 默认是，暂时不支持前台用户</param>
        /// <returns></returns>
        public static bool HasRights(this Page thePage, RightTypes rights, bool isBack = true)
        {
            return HasRights(thePage, (long)rights, isBack);
        }
        /// <summary>
        /// 判断当前用户是否有权限
        /// </summary>
        /// <param name="thePage"></param>
        /// <param name="rights">权限</param>
        /// <param name="isBack">是否后台用户 默认是，暂时不支持前台用户</param>
        /// <returns></returns>
        public static bool HasRights(this Page thePage, long rights, bool isBack = true)
        {
            return true;
        }
        public static bool HasRights(this HttpRequest request, long rights, bool isBack = true)
        {
            return true;
        }
        /// <summary>
        /// 判断当前用户是否有“查看”权限，如果没有权限则自动跳转
        /// </summary>
        /// <param name="thePage"></param>
        /// <param name="isBack"></param>
        /// <param name="js">使用JS跳转</param>
        /// <returns>有权限返回True</returns>
        public static bool CheckAndRedirect(this Page thePage, bool isBack = true, bool js = true)
        {
            return CheckAndRedirect(thePage, 0x01, isBack, js);
        }
        /// <summary>
        /// 判断当前用户是否有权限，如果没有权限则自动跳转
        /// </summary>
        /// <param name="thePage"></param>
        /// <param name="rights">权限</param>
        /// <param name="isBack">是否后台用户 默认是，暂时不支持前台用户</param>
        /// <param name="js">使用JS跳转</param>
        /// <returns>有权限返回True</returns>
        public static bool CheckAndRedirect(this Page thePage, RightTypes rights, bool isBack = true, bool js = true)
        {
            return CheckAndRedirect(thePage, (long)rights, isBack, js);
        }

        /// <summary>
        /// 判断当前用户是否有权限，如果没有权限则自动跳转
        /// </summary>
        /// <param name="thePage"></param>
        /// <param name="rights">权限</param>
        /// <param name="isBack">是否后台用户 默认是，暂时不支持前台用户</param>
        /// <param name="js">使用JS跳转</param>
        /// <returns>有权限返回True</returns>
        public static bool CheckAndRedirect(this Page thePage, long rights, bool isBack = true, bool js = true)
        {
            if (HasRights(thePage, rights, isBack)) return true;
            NoAccess(thePage, js);
            return false;
        }
      
        public static void NoAccess(this Page thePage, bool js = true)
        {
            if (js)
            {
                thePage.ClientScript.RegisterStartupScript(thePage.GetType(), "NOACCESS", "location.href = ('/Admin/NoAccess.htm');", true);
                thePage.Response.End();
            }
            else
            {
                thePage.Response.Redirect("/Admin/NoAccess.htm", true);
            }
        }
        /// <summary>
        /// 判断指定用户是否有权限
        /// </summary>
        /// <param name="staffNo"></param>
        /// <param name="prgItem">目标</param>
        /// <param name="rights">权限</param>
        /// <param name="isBack">是否后台用户 默认是，暂时不支持前台用户</param>
        /// <returns></returns>
        public static bool HasRights(string staffNo, string prgItem, long rights, bool isBack = true)
        {
            return true;
        }
    }
}
